//
//  XYSettingViewController.m
//  NemoSDKDemo
//
//  Created by 高超 on 2020/7/29.
//  Copyright © 2020 JackYang. All rights reserved.
//

#import "XYSettingViewController.h"

@interface XYSettingViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property (weak, nonatomic) IBOutlet UILabel *versionLabel;

@end

@implementation XYSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"设置";
    [self _setupSubView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (BOOL)shouldAutorotate{
    //不允许转屏
    return NO;
}

#pragma mark private function
- (void)_setupSubView {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20, 0, 42, 40)];
    view.backgroundColor = [UIColor clearColor];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    button.frame = CGRectMake(5, 0, 42, 35);
    [button addTarget:self action:@selector(_goBack) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc]initWithCustomView:view];
    self.navigationItem.leftBarButtonItem = backButtonItem;
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    
    UIBarButtonItem *saveBtn = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveDisplayName)];
    self.navigationItem.rightBarButtonItem = saveBtn;
    
    self.nameTF.text = [XYSDKManager sharedInstance].user.displayName;
    self.versionLabel.text = [[XYSDKManager sharedInstance] sdkVersion];
}

- (void)_goBack {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark IBAction
- (IBAction)tapViewAction:(id)sender {
    [self.nameTF resignFirstResponder];
}

- (void)saveDisplayName {
    if ([_nameTF.text isEqualToString:[XYSDKManager sharedInstance].user.displayName]) {
        [XYHUDManager showBriefToast:@"与原名称相同"];
        return;
    }
    [[XYSDKManager sharedInstance] modifyDisplayName:_nameTF.text completion:^(BOOL success) {
        if (success) {
            [XYHUDManager showBriefToast:@"修改成功"];
            [XYSDKManager sharedInstance].user.displayName = _nameTF.text;
        }
    }];
}

@end
